package com.logger.export.csv;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.logger.LogFile;
import com.logger.LogFiles;
import com.logger.LogQuery;
import com.logger.LogTransaction;

public class CsvExport {
    private final CSVFileWriter csvFileWriter;
    private boolean             titleAdded;
    private boolean             fileClosed;
    private boolean             linesAdded;
    private final List<File>    files;

    public CsvExport(final String csvFileName) {
        this.csvFileWriter = new CSVFileWriter(csvFileName);
        this.files = new ArrayList<File>();
    }

    public void addTittle() {
        if (this.titleAdded) {
            return;
        }
        final CSVLine csvLine = new CSVLine();
        csvLine.add("USER NAME");
        csvLine.add("TIME HOUR");
        csvLine.add("TIME MINUTE");
        csvLine.add("TIME SECONDS");
        csvLine.add("TRANSACTION");
        csvLine.add("TRANSACTION STATE");
        csvLine.add("QUERY FILE");
        csvLine.add("QUERY NAME");
        csvLine.add("CONSTRUCTION TIME");
        csvLine.add("EXECUTION TIME");
        csvLine.add("ROWS NUMBER");
        csvLine.add("QUERY PREVIEW");
        csvLine.add("SESSION ID");
        csvLine.add("NODE");
        this.csvFileWriter.writeLine(csvLine);
        this.titleAdded = true;
    }

    public boolean appendLogFile(final LogFile logFile) {
        if (this.fileClosed) {
            return false;
        }
        if (!this.titleAdded) {
            this.addTittle();
        }
        final String userName = logFile.getUserName();
        final String sessionId = logFile.getSessionId();
        final String node = logFile.getNode();
        final SimpleDateFormat simpleDateFormatH = new SimpleDateFormat("HH");
        final SimpleDateFormat simpleDateFormatM = new SimpleDateFormat("mm");
        final SimpleDateFormat simpleDateFormatS = new SimpleDateFormat("ss");
        final String timeH = simpleDateFormatH.format(logFile.getDate());
        final String timeM = simpleDateFormatM.format(logFile.getDate());
        final String timeS = simpleDateFormatS.format(logFile.getDate());
        for (final LogQuery logQuery : logFile.getListLogQuery()) {
            final LogTransaction transaction = logQuery.getTransaction();
            final String sTransaction;
            final String sTransactionState;
            if (transaction == null) {
                sTransaction = "WITHOUT";
                sTransactionState = "NO STATE";
            } else {
                sTransaction = String.valueOf(transaction.getId());
                sTransactionState = transaction.getCommit() == null ? "ERROR" : transaction.getCommit() ? "COMMIT" : "ROLLBACK";
            }
            final String queryfile = logQuery.getQueryfile();
            final String queryName = logQuery.getQueryName();
            final String constructionTime = logQuery.getConstructionTime() == null ? "ERROR" : String.valueOf((double) logQuery.getConstructionTime() / 1000);
            final String executionTime = String.valueOf((double) logQuery.getExecutionTime() / 1000);
            final String rowsNumber = String.valueOf(logQuery.getRowsNumber());
            final String queryPreview = logQuery.getRawQueryContentPreview();
            final CSVLine csvLine = new CSVLine();
            csvLine.add(userName);
            csvLine.add(timeH);
            csvLine.add(timeM);
            csvLine.add(timeS);
            csvLine.add(sTransaction);
            csvLine.add(sTransactionState);
            csvLine.add(queryfile);
            csvLine.add(queryName);
            csvLine.add(constructionTime);
            csvLine.add(executionTime);
            csvLine.add(rowsNumber);
            csvLine.add(queryPreview);
            csvLine.add(sessionId);
            csvLine.add(node);

            this.csvFileWriter.writeLine(csvLine);
        }
        if (!this.linesAdded) {
            this.linesAdded = true;
        }
        return true;
    }

    public boolean close() {
        if (this.fileClosed) {
            return true;
        }
        if (this.linesAdded) {
            this.csvFileWriter.close();
            return true;
        }
        return false;
    }

    public boolean writeAllFiles(final LogFiles _logFiles) {
        this.addTittle();
        for (final LogFile logFile : _logFiles) {
            this.appendLogFile(logFile);
        }
        return true;
    }

    public List<File> getFiles() {
        return this.files;
    }

}
